package com.dgshanger.sanhxiaofeisc.ui.login

import android.animation.ValueAnimator
import android.content.Intent
import android.support.v4.content.ContextCompat
import android.support.v4.view.PagerAdapter
import android.support.v4.view.ViewPager
import android.text.InputFilter
import android.text.InputType
import android.text.method.PasswordTransformationMethod
import android.view.KeyEvent
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.blankj.utilcode.util.ActivityUtils
import com.dgshanger.sanhxiaofeisc.MainActivity
import com.dgshanger.sanhxiaofeisc.R
import com.dgshanger.sanhxiaofeisc.mvp.contract.LoginContract
import com.xx.baseuilibrary.mvp.BaseMvpActivity
import com.xxl.sodmall.mvp.presenter.LoginPresenter
import com.xxl.sodmall.ui.RegisterActivity
import kotlinx.android.synthetic.main.activity_login.*
import kotlinx.android.synthetic.main.view_quick_login.*
import kotlinx.android.synthetic.main.view_quick_login.view.*


/**
 * LoginActivity
 * 沉迷学习不能自拔
 * Describe：登录页面
 * Created by 雷小星🍀 on 2018/2/1 15:42.
 */
class LoginActivity : BaseMvpActivity<LoginContract.Model, LoginContract.View, LoginPresenter>(), LoginContract.View {

    override fun openSwipeBack(): Boolean = false

    override fun getLoginType(): Int = loginType

    private var loginType = QUICK_LOGIN

    companion object {
        const val QUICK_LOGIN = 0
        const val PASSWORD_LOGIN = 1
    }


    override fun setSendBtnEnable(enable: Boolean) {
        tv_send_code.isEnabled = enable
    }

    override fun setSendBtnText(text: String?) {
        tv_send_code.text = text
    }

    override fun getPhone(): String {
        return (if (loginType == QUICK_LOGIN) viewQuickLogin else viewPasswordLogin)
                ?.et_phone!!.text.toString().trim()
    }

    override fun getVCode(): String {
        return (if (loginType == QUICK_LOGIN) viewQuickLogin else viewPasswordLogin)
                ?.et_code!!.text.toString().trim()
    }

    override fun loginSuccess() {
        val intent = Intent(mContext, MainActivity::class.java)
        startActivity(intent)
        finish()
        overridePendingTransition(R.anim.scale_in, R.anim.scale_out)
    }

    override fun createPresenter(): LoginPresenter = LoginPresenter()

    override fun getActivityLayoutId(): Int = R.layout.activity_login

    override fun initEvent() {

        tv_quick_login.setOnClickListener {
            //快捷登录
            swithcLoginType(true)
        }
        tv_password_login.setOnClickListener {
            //账号密码登录
            swithcLoginType(false)
        }
        tv_register.setOnClickListener {
            startActivity(RegisterActivity::class.java)
        }

        tv_forgetpassword.setOnClickListener {
            startActivity(ForgetPwActivity::class.java)
        }

        btn_login.setOnClickListener {
            //登录
            getPresenter().login()
        }

        viewPager.addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
            override fun onPageScrollStateChanged(state: Int) {
            }

            override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {

            }

            override fun onPageSelected(position: Int) {
                swithcLoginType(position == 0)
            }

        })

    }

    override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            //拦截返回键,退出应用程序
            ActivityUtils.finishAllActivities()
            return true
        }
        return super.onKeyDown(keyCode, event)
    }

    private fun swithcLoginType(quickLogin: Boolean) {
        if (quickLogin) {
            //快捷登录
            if (loginType == QUICK_LOGIN)
                return
            loginType = QUICK_LOGIN
            viewPager.currentItem = 0

            ValueAnimator.ofFloat(13f, 18f)
                    .apply {
                        duration = 400
                        addUpdateListener(object : ValueAnimator.AnimatorUpdateListener {
                            override fun onAnimationUpdate(animation: ValueAnimator?) {
                                tv_quick_login.textSize = animation!!.animatedValue as Float
                            }
                        })
                    }
                    .start()
            tv_password_login.setTextColor(ContextCompat.getColor(mContext, R.color.colorAccent))
            tv_password_login.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, R.drawable.dian_)

            ValueAnimator.ofFloat(18f, 13f)
                    .apply {
                        duration = 400
                        addUpdateListener(object : ValueAnimator.AnimatorUpdateListener {
                            override fun onAnimationUpdate(animation: ValueAnimator?) {
                                tv_password_login.textSize = animation!!.animatedValue as Float
                            }
                        })
                    }
                    .start()

            tv_quick_login.setTextColor(ContextCompat.getColor(mContext, R.color.colorAccent))
            tv_quick_login.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, R.drawable.dian_)

            tv_password_login.setTextColor(ContextCompat.getColor(mContext, R.color.colorAccentLight))
            tv_password_login.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0)

        } else {
            //密码登录
            if (loginType == PASSWORD_LOGIN)
                return
            loginType = PASSWORD_LOGIN
            viewPager.currentItem = 1
            ValueAnimator.ofFloat(13f, 18f)
                    .apply {
                        duration = 400
                        addUpdateListener(object : ValueAnimator.AnimatorUpdateListener {
                            override fun onAnimationUpdate(animation: ValueAnimator?) {
                                tv_password_login.textSize = animation!!.animatedValue as Float
                            }
                        })
                    }
                    .start()
            tv_password_login.setTextColor(ContextCompat.getColor(mContext, R.color.colorAccent))
            tv_password_login.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, R.drawable.dian_)

            ValueAnimator.ofFloat(18f, 13f)
                    .apply {
                        duration = 400
                        addUpdateListener(object : ValueAnimator.AnimatorUpdateListener {
                            override fun onAnimationUpdate(animation: ValueAnimator?) {
                                tv_quick_login.textSize = animation!!.animatedValue as Float
                            }
                        })
                    }
                    .start()
            tv_quick_login.setTextColor(ContextCompat.getColor(mContext, R.color.colorAccentLight))
            tv_quick_login.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0)
        }
    }

    private var viewQuickLogin: View? = null
    private var viewPasswordLogin: View? = null
    override fun initData() {

        viewPager.adapter = object : PagerAdapter() {

            override fun isViewFromObject(view: View, `object`: Any): Boolean = view == `object`

            override fun getCount(): Int = 2

            override fun instantiateItem(container: ViewGroup, position: Int): Any {
                if (position == 0) {
                    //快捷登录
                    if (viewQuickLogin == null) {
                        viewQuickLogin = LayoutInflater.from(container.context).inflate(R.layout.view_quick_login, null)
                        viewQuickLogin!!.tv_login_phone.text = getString(R.string.Mobile_phone_number)
                        viewQuickLogin!!.tv_login_code.text = getString(R.string.TAC)
                        viewQuickLogin!!.et_code.inputType = InputType.TYPE_CLASS_NUMBER//设置输入类型为数字
                        viewQuickLogin!!.et_code.hint = getString(R.string.please_enter_ATC)
                        viewQuickLogin!!.et_code.filters = arrayOf(InputFilter.LengthFilter(6))//限制长度为6
                        viewQuickLogin!!.tv_send_code.visibility = View.VISIBLE
                        viewQuickLogin!!.tv_send_code.setOnClickListener {
                            //发送验证码
                            getPresenter().sendCode()
                        }
                    }
                    container.addView(viewQuickLogin)
                    return viewQuickLogin as View
                } else {
                    //密码登录
                    if (viewPasswordLogin == null) {
                        viewPasswordLogin = LayoutInflater.from(container.context).inflate(R.layout.view_quick_login, null)
                        viewPasswordLogin!!.tv_login_phone.text = getString(R.string.account_number)
                        viewPasswordLogin!!.tv_login_code.text = getString(R.string.password)
                        viewPasswordLogin!!.et_code.transformationMethod = PasswordTransformationMethod.getInstance()
                        viewPasswordLogin!!.et_code.hint = getString(R.string.please_input_passwrod)
                        viewPasswordLogin!!.tv_send_code.visibility = View.INVISIBLE
                    }
                    container.addView(viewPasswordLogin)
                    return viewPasswordLogin as View
                }
            }

            override fun destroyItem(container: ViewGroup, position: Int, `object`: Any) {
                if (position == 0) {
                    container.removeView(viewQuickLogin)
                } else {
                    container.removeView(viewPasswordLogin)
                }
            }
        }
    }
}